/*2S7LXljdWdGhrCxZ1MFl4i0/jcYg0CI/M/l35xRmsHHDM270ypZGxQFjXbKKRAXA*/
import React, { Component } from 'react';
import {high,base,ajax,toast} from 'nc-lightapp-front';
import ReferLoader from '../../ReferLoader';
const { NCFormControl: FormControl, NCDatePicker: DatePicker, NCButton: Button, NCRadio: Radio, NCBreadcrumb: Breadcrumb,
    NCRow: Row, NCCol: Col, NCTree: Tree, NCIcon: Icon, NCLoading: Loading, NCTable: Table, NCSelect: Select,
    NCCheckbox: Checkbox, NCNumber, AutoComplete, NCDropdown: Dropdown, NCPanel: Panel,NCModal:Modal,NCForm
} = base;
const {  NCFormItem:FormItem } = NCForm;
const format = 'YYYY-MM-DD';
const timeFormat = 'YYYY-MM-DD HH:mm:ss';
import {SelectItem} from '../FormItems';
import {InputItem} from '../../../public/components/FormItems/index'
import {onEnterAction} from '../assidDbModal/assidOnEnter';
import {constClassId} from "../assidDbModalVersion/const.js";

/**
 * 
 * @param {*} self 
 * @param {*} text 
 * @param {*} record 
 * @param {*} index 
 * @param {*} showDisableData 
 * @param {*} ruleCrossCheck 是否走科目交叉校验 
 */
export default function drawingAssidData(self,text, record, index,showDisableData, accAssRuleCtrl = true){
    let {assData,childAssData}=self.state;
    let defaultValue;
    if(self.props.isNeedVersion){
        let trueRefpk=constClassId[record.classid]?record.pk_vid:record.pk_Checkvalue;
        defaultValue = { refname: record.checkvaluename, refpk: trueRefpk };
    }else{
        defaultValue = { refname: record.checkvaluename, refpk: record.pk_Checkvalue };
    }
    let tempVal = defaultValue.refname =='~' ? '' : defaultValue.refname //日期时间、日期有时返回'~'
    // let defaultValue = [{ refname: record.checkvaluename, refpk: record.pk_Checkvalue }];
    let originData = self.findByKey(record.key, assData);
    if(record.refnodename){
        let referUrl;
        //需要多版本参照
        if(self.props.isNeedVersion){
            referUrl= self.changeRefUrl(record);
        }else{
            referUrl= record.refnodename+'.js';
        }
        let tmpPrepareddate=childAssData.prepareddate;
        //不带时分秒 参照获取数据有问题
        if(tmpPrepareddate&&tmpPrepareddate.length==10){
            tmpPrepareddate+=' 00:00:00'
        }
        let config={
            "pk_org": childAssData.pk_org,
            "pk_unit": childAssData.pk_org,/* 业务单元 pk 用来进行 科目交叉校验规则过滤 */
            // "pk_defdoclist": record.pk_defdoclist,
            "pk_defdoclist":record.classid,
            "pk_accountingbook": childAssData.pk_accountingbook,
            "date": childAssData.prepareddate,
            "pk_accasoa": childAssData.pk_accasoa,
            "classid": record.classid,
            "assvo": JSON.stringify(assData),
            "isDataPowerEnable": 'Y',
            "DataPowerOperationCode" : 'fi',
            //以下为多版本参照需要日期 
            VersionStartDate:tmpPrepareddate, //部门,利润中心
            queryDate:tmpPrepareddate    //客户,客商,供应商
            // "GridRefActionExt": 'nccloud.web.gl.ref.AccAssRuleCtrlSqlBuilder',
            // "TreeRefActionExt": 'nccloud.web.gl.ref.AccAssRuleCtrlSqlBuilder'
        }
            if(record.classid=='b26fa3cb-4087-4027-a3b6-c83ab2a086a9'||record.classid=='40d39c26-a2b6-4f16-a018-45664cac1a1f'){//部门，人员
                return (
                    <div
                        onKeyDown={(event) => {
                            let referPopWindowFlag=true;
                            let referPopWindowArr =document.getElementsByClassName('refer-pop-window');
                            for(let i=0;i<referPopWindowArr.length;i++){
                                if(referPopWindowArr[i].style.display=='flex'){
                                    referPopWindowFlag=false;
                                    break;
                                }
                            }
                            if(event.keyCode=='9'&&assData.length-1==index&&referPopWindowFlag ){
                                        event.stopPropagation();
                                        event.preventDefault();
                                    let modelNode= ReactDOM.findDOMNode(self);
                                    let inputNode=modelNode&&modelNode.querySelector('#assDataModal input.refer-input:first-child');
                                    inputNode&&inputNode.focus()
                            }
                        }}
                        id={`hot-key-${self.moduleId}-checkvaluename-${index}`}
                    >
                        <ReferLoader
                            onEnter={(event)=>{
                                onEnterAction(event,self,'checkvaluename',`hot-key-${self.moduleId}-checkvaluename-${index}`,index,record)
                            }}
                            fieldid='checkvaluename'
                            tag={record.refnodename}
                            refcode={referUrl}
                            disabled={record.editFlag?record.editFlag:false}
                            value={defaultValue}
                            isShowUnit={true}
                            unitProps={{
                                refType: 'tree',
                                refName: self.state.json['publiccomponents-000011'],/* 国际化处理： 业务单元*/
                                refCode: 'uapbd.refer.org.BusinessUnitTreeRef',
                                rootNode:{refname:self.state.json['publiccomponents-000011'],refpk:'root'},/* 国际化处理： 业务单元*/
                                placeholder:self.state.json['publiccomponents-000011'],/* 国际化处理： 业务单元*/
                                queryTreeUrl: '/nccloud/uapbd/org/BusinessUnitTreeRef.do',
                                treeConfig:{name:[self.state.json['publiccomponents-000012'], self.state.json['publiccomponents-000013']],code: ['refcode', 'refname']},/* 国际化处理： 编码,名称*/
                                isMultiSelectedEnabled: false,
                                //unitProps:unitConf,
                                isShowUnit:false
                            }}
                            unitCondition={{
                                pk_financeorg:childAssData.pk_org,
                                'TreeRefActionExt':'nccloud.web.gl.ref.OrgRelationFilterRefSqlBuilder'
                            }}
                            isMultiSelectedEnabled={false}
                            isShowDisabledData={showDisableData}
                            unitValueIsNeeded={false}
                            isShowDimission={true}
                            queryCondition={(obj) => {
                                if(obj){
                                    if(accAssRuleCtrl){
                                        setAccAssRuleCheckSqlBuilder(config, obj.refType);
                                    }
                                }
                                if(record.classid == '40d39c26-a2b6-4f16-a018-45664cac1a1f'){
                                    //显示离职人员
                                    config.isShowDimission=true;
                                }
                                if(record.classid&&record.classid.length==20){//classid的长度大于20的话过滤条件再加一个pk_defdoclist
                                    return config
                                }else{
                                    config.busifuncode="all";
                                    return config
                                }   
                            }}
                            onChange={(v)=>{
                                let { assData } =self.state;
                                let originData = self.findByKey(record.key, assData);
                                if (originData) {
                                    originData.checkvaluename = v.refname || '';
                                    originData.checkvaluecode=v.refcode || '';
                                    if(self.props.isNeedVersion){
                                        if(constClassId[record.classid]){
                                            originData.pk_vid = v.refpk || '';
                                        }
                                        originData.pk_Checkvalue=self.getReferValOfVersion(record,v);
                                    }else{
                                        originData.pk_Checkvalue = v.refpk || '';
                                    }
                                } 
                                childAssData.assData=assData;  
                                self.setState({
                                assData,childAssData
                                })
                            }}
                        />
                    </div>
                )
            }else{
                let isTreelazyLoad = record.classid == '8c6510dd-3b8a-4cfc-a5c5-323d53c6006f'; //客商参照设置懒加载
				let isProfitCenter = record.classid == 'eca72cac-085b-4796-b433-1b17b754574c'; //利润中心
				let pk_unitFlag=record.classid=="985be8a4-3a36-4778-8afe-2d8ed3902659";//业务单元
                return (
                    <div 
                        onKeyDown={(event) => {
                            let referPopWindowFlag=true;
                            let referPopWindowArr =document.getElementsByClassName('refer-pop-window');
                            for(let i=0;i<referPopWindowArr.length;i++){
                                if(referPopWindowArr[i].style.display=='flex'){
                                    referPopWindowFlag=false;
                                    break;
                                }
                            }
                            if(event.keyCode=='9'&&assData.length-1==index&&referPopWindowFlag ){
                                event.stopPropagation();
                                event.preventDefault();
                                let modelNode= ReactDOM.findDOMNode(self);
                                let inputNode=modelNode&&modelNode.querySelector('#assDataModal input.refer-input:first-child');
                                inputNode&&inputNode.focus()
                            }
                        }}
                        id={`hot-key-${self.moduleId}-checkvaluename-${index}`}
                    >
                        <ReferLoader
                            onEnter={(event)=>{
                                onEnterAction(event,self,'checkvaluename',`hot-key-${self.moduleId}-checkvaluename-${index}`,index,record)
                            }}
                            fieldid='checkvaluename'
                            tag={record.refnodename}
                            refcode={referUrl}
                            disabled={record.editFlag?record.editFlag:false}
                            value={defaultValue}
                            isMultiSelectedEnabled={false}
                            isTreelazyLoad={isTreelazyLoad}
                            isShowDisabledData={showDisableData}
                            queryCondition={(obj) => {
                                if(obj){
                                    if(accAssRuleCtrl){
                                        setAccAssRuleCheckSqlBuilder(config, obj.refType);
                                    }
                                }
                                if(pk_unitFlag){
                                    config.pk_org=null;
                                    config.pk_unit=null;
                                }
                                if(isProfitCenter){
                                    config.financeorg = config.pk_org;
                                }
                                return config 
                            }}
                            onChange={(v)=>{
                                let { assData } =self.state;
                                let originData = self.findByKey(record.key, assData);
                                if (originData) {
                                    originData.checkvaluename = v.refname || '';
                                    originData.checkvaluecode=v.refcode || '';
                                    if(self.props.isNeedVersion){
                                        if(constClassId[record.classid]){
                                            originData.pk_vid = v.refpk || '';
                                        }
                                        originData.pk_Checkvalue=self.getReferValOfVersion(record,v);
                                    }else{
                                        originData.pk_Checkvalue = v.refpk || '';
                                    }
                                } 
                                childAssData.assData=assData;  
                                self.setState({
                                assData,childAssData
                                })
                            }}
                        />
                    </div>
                )

            }
    }else{//不是参照的话要区分日期、字符、数值
        if(record.classid=='BS000010000100001033'){//日期
            return(
                <div id={`hot-key-${self.moduleId}-checkvaluename-${index}`}>
                    <DatePicker
                        onEnter={(event)=>{
                            onEnterAction(event,self,'checkvaluename',`hot-key-${self.moduleId}-checkvaluename-${index}`,index,record)
                        }}
                        fieldid='checkvaluename'
                        className = 'userdefpicker'
                        //name={item.itemKey}
                        format={format}
                        type="customer"
                        isRequire={true}
                        disabled={record.editFlag?record.editFlag:false}
                        // placeholder={dateInputPlaceholder}
                        value={tempVal}
                        onChange={(v) => {
                            let { assData } =self.state;
                            let originData = self.findByKey(record.key, assData);
                            if (originData) {
                                originData.checkvaluename = v;
                                originData.pk_Checkvalue =v;
                                originData.checkvaluecode=v;
                            }
                            childAssData.assData=assData; 
                            self.setState({
                            assData,childAssData
                            })
                        }}
                    />
                </div>
            )
        }else if(record.classid=='BS000010000100001034'){//日期时间
            return(
                <div id={`hot-key-${self.moduleId}-checkvaluename-${index}`}>
                    <DatePicker
                        onEnter={(event)=>{
                            onEnterAction(event,self,'checkvaluename',`hot-key-${self.moduleId}-checkvaluename-${index}`,index,record)
                        }}
                        className = 'userdefpicker'
                        fieldid='checkvaluename'
                        //name={item.itemKey}
                        showTime={true}
                        disabled={record.editFlag?record.editFlag:false}
                        format={timeFormat}
                        type="customer"
                        isRequire={true}
                        // placeholder={dateInputPlaceholder}
                        value={tempVal}
                        onChange={(v) => {
                            let { assData } =self.state;
                            let originData = self.findByKey(record.key, assData);
                            if (originData) {
                                originData.checkvaluename = v;
                                originData.pk_Checkvalue =v;
                                originData.checkvaluecode=v;
                            }
                            childAssData.assData=assData; 
                            self.setState({
                            assData,childAssData
                            })
                        }}
                    />
                </div>
            )
        }else if(record.classid=='BS000010000100001031' ||record.classid=='BS000010000100001004'){//数值
            return(
                <div id={`hot-key-${self.moduleId}-checkvaluename-${index}`}>
                    <NCNumber
                        onEnter={(event)=>{
                            onEnterAction(event,self,'checkvaluename',`hot-key-${self.moduleId}-checkvaluename-${index}`,index,record)
                        }}
                        fieldid='checkvaluename'
                        scale={Number(record.digits?record.digits:'0')}
                        disabled={record.editFlag?record.editFlag:false}
                        value={defaultValue.refpk || defaultValue.refname}
                        maxlength={Number(record.inputlength)}
                        placeholder={self.state.json['publiccomponents-000004']}/* 国际化处理： 请输入数字*/
                        onChange={(v)=>{
                            let { assData } =self.state;
                            let originData = self.findByKey(record.key, assData);
                            if (originData) {
                                originData.checkvaluename = v;
                                originData.pk_Checkvalue =v;
                                originData.checkvaluecode=v;
                            }
                            childAssData.assData=assData; 
                            self.setState({
                            assData,childAssData
                            })
                        }}
                    />
                </div>
            )
        }else if(record.classid=='BS000010000100001032'){//布尔
            return(
                <div id={`hot-key-${self.moduleId}-checkvaluename-${index}`}>
                    <SelectItem 
                        onEnter={(event)=>{
                            onEnterAction(event,self,'checkvaluename',`hot-key-${self.moduleId}-checkvaluename-${index}`,index,record)
                        }}
                        name={record.checktypecode}
                        fieldid='checkvaluename'
                        disabled={record.editFlag?record.editFlag:false}
                        defaultValue={defaultValue.refname} 
                        items = {
                            () => {
                                return ([{
                                    label: self.state.json['publiccomponents-000005'],/* 国际化处理： 是*/
                                    value: 'Y'
                                }, {
                                    label: self.state.json['publiccomponents-000006'],/* 国际化处理： 否*/
                                    value: 'N'
                                }]) 
                            }
                        }
                        onChange={(v)=>{
                            let { assData } =self.state;
                            let originData = self.findByKey(record.key, assData);
                            if (originData) {
                                originData.checkvaluename = v;
                                originData.pk_Checkvalue =v;
                                originData.checkvaluecode=v;
                                }
                            childAssData.assData=assData; 
                            self.setState({
                            assData,childAssData
                            })
                        }}
                    />
                </div>
            )
        }else{//字符
            return(
                // <FormControl
				// 					value={defaultValue?defaultValue.refname:defaultValue.refname}
				// 					onChange={(v) => {
                //                         if(v&&v.length>0){
                //                             if(GetChinese(v).length>0&&2*(GetChinese(v).length)>Number(record.inputlength)){
                //                                 v=v.slice(0,Number(record.inputlength/2));
                //                             }else if(GetChinese(v).length>0&&2*(GetChinese(v).length)+RemoveChinese(v).length>Number(record.inputlength)){
                //                                 toast({content:self.state.json['publiccomponents-000148']+record.inputlength,color:'warning'});//输入的数字长度不能超过用户设置的长度
                //                                 v= RemoveChinese(v);
                //                             }else if(RemoveChinese(v).length>Number(record.inputlength)){
                //                                 toast({content:self.state.json['publiccomponents-000148']+record.inputlength,color:'warning'});//输入的数字长度不能超过用户设置的长度
                //                                 v=v.slice(0,Number(record.inputlength));
                //                             }
                //                         }
				// 						let { assData } =self.state;
                //                         let originData = self.findByKey(record.key, assData);
                //                         if (originData) {
                //                             originData.checkvaluename = v;
                //                             originData.pk_Checkvalue =v;
                //                             originData.checkvaluecode=v;
                //                             }
                //                         childAssData.assData=assData; 
                //                         self.setState({
                //                         assData,childAssData
                //                         })
				// 					}}
                // 				/>
                <div id={`hot-key-${self.moduleId}-checkvaluename-${index}`}>
                    <InputItem
                        onEnter={(event)=>{
                            onEnterAction(event,self,'checkvaluename',`hot-key-${self.moduleId}-checkvaluename-${index}`,index,record)
                        }}
                        fieldid='checkvaluename'
                        disabled={record.editFlag?record.editFlag:false}
                        defaultValue={defaultValue.refname || defaultValue.refpk}
                        maxlength={record && record.inputlength || 10}
                        onChange={(v)=>{
                            let { assData } =self.state;
                            let originData = self.findByKey(record.key, assData);
                            if (originData) {
                                originData.checkvaluename = v;
                                originData.pk_Checkvalue =v;
                                originData.checkvaluecode=v;
                                }
                            childAssData.assData=assData; 
                            self.setState({
                            assData,childAssData
                            })
                        }}
                    />
                </div>
            )
        }
        
    }
} 

/**
 * 设置科目交叉校验规则sqlbuilder
 * @param {*} config 
 */
function setAccAssRuleCheckSqlBuilder(config, refType){
    if(refType=='grid'){
        config.GridRefActionExt='nccloud.web.gl.ref.AccAssRuleCtrlSqlBuilder';
    }else if(refType=='tree'){
        config.TreeRefActionExt='nccloud.web.gl.ref.AccAssRuleCtrlSqlBuilder';
    }else if(refType=='gridTree'){
        config.GridRefActionExt='nccloud.web.gl.ref.AccAssRuleCtrlSqlBuilder';
    }
}
/*2S7LXljdWdGhrCxZ1MFl4i0/jcYg0CI/M/l35xRmsHHDM270ypZGxQFjXbKKRAXA*/